<template>
  <el-dialog title="新增员工" :visible="dialogAddEmpVisible">
    <el-form ref="addEmpForm" style="width:70%" :model="form" :rules="rules">
      <el-form-item prop="username" label="姓名" :label-width="formLabelWidth">
        <el-input v-model="form.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item prop="mobile" label="手机" :label-width="formLabelWidth">
        <el-input v-model="form.mobile" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item
        prop="timeOfEntry"
        label="入职时间"
        :label-width="formLabelWidth"
      >
        <el-date-picker
          style="width:100%"
          v-model="form.timeOfEntry"
          type="date"
          placeholder="选择日期"
        >
        </el-date-picker>
        <!-- <el-input v-model="form.timeOfEntry" autocomplete="off"></el-input> -->
      </el-form-item>
      <el-form-item
        prop="formOfEmployment"
        label="聘用形式"
        :label-width="formLabelWidth"
      >
        <el-select
          style="width:100%"
          v-model="form.formOfEmployment"
          placeholder="请选择"
        >
          <el-option label="正式" value="1"></el-option>
          <el-option label="非正式" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        prop="workNumber"
        label="工号"
        :label-width="formLabelWidth"
      >
        <el-input v-model="form.workNumber" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item
        prop="departmentName"
        label="部门"
        :label-width="formLabelWidth"
      >
        <el-input
          @focus="treeBoxFlag = true"
          v-model="form.departmentName"
          autocomplete="off"
        ></el-input>
        <div
          v-show="treeBoxFlag"
          class="tree-box"
          style="height:200px;overflow:auto;border:2px  solid  red "
        >
          <el-tree
            default-expand-all
            :data="data"
            :props="defaultProps"
            @node-click="handleNodeClick"
          ></el-tree>
        </div>
      </el-form-item>
      <el-form-item
        prop="correctionTime"
        label="转正时间"
        :label-width="formLabelWidth"
      >
        <el-date-picker
          style="width:100%"
          v-model="form.correctionTime"
          type="date"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="addEmploy">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { departmentApi } from "@/api/departments";
import { tranListToTreeData } from "@/utils";

import { addEmployees } from "@/api/employee";
export default {
  name: "",
  props: {
    dialogAddEmpVisible: {
      type: Boolean
    }
  },
  watch: {
    "form.departmentName"(newVal) {
      console.log(newVal, 33333);
      if (newVal.length) {
        this.treeBoxFlag = false;
      }
    }
  },
  data() {
    return {
      treeBoxFlag: false,
      dialogFormVisible: false,
      form: {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "123",
        timeOfEntry: "",
        correctionTime: ""
      },
      formLabelWidth: "120px",
      /* 校验规则 */
      rules: {
        username: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        formOfEmployment: [
          { required: true, message: "聘用形式不能为空", trigger: "blur" }
        ],
        workNumber: [
          { required: true, message: "工号不能为空", trigger: "blur" }
        ],
        departmentName: [
          { required: true, message: "组织名称不能为空", trigger: "blur" }
        ],
        timeOfEntry: [
          { required: true, message: "入职时间不能为空", trigger: "blur" }
        ],
        correctionTime: [
          { required: true, message: "转正时间不能为空", trigger: "blur" }
        ]
      },

      /* 树形组件 */
      data: [
        {
          label: "一级 1",
          children: [
            {
              label: "二级 1-1",
              children: [
                {
                  label: "三级 1-1-1"
                }
              ]
            }
          ]
        },
        {
          label: "一级 2",
          children: [
            {
              label: "二级 2-1",
              children: [
                {
                  label: "三级 2-1-1"
                }
              ]
            },
            {
              label: "二级 2-2",
              children: [
                {
                  label: "三级 2-2-1"
                }
              ]
            }
          ]
        },
        {
          label: "一级 3",
          children: [
            {
              label: "二级 3-1",
              children: [
                {
                  label: "三级 3-1-1"
                }
              ]
            },
            {
              label: "二级 3-2",
              children: [
                {
                  label: "三级 3-2-1"
                }
              ]
            }
          ]
        }
      ],
      defaultProps: {
        children: "children",
        label: "name"
      }
    };
  },
  created() {
    this.getDepartmentList();
  },
  methods: {
    //  获取所有的数据列表
    async getDepartmentList() {
      //console.log(11111);
      const res = await departmentApi();
      //console.log(res, 3333);
      this.data = tranListToTreeData(res.depts, "");
    },

    handleNodeClick(data) {
      console.log(data, 22222);
      this.form.departmentName = data.name;
      //   this.treeBoxFlag = false;
    },

    /* 点击添加按钮 */
    addEmploy() {
      this.$refs.addEmpForm.validate(async valid => {
        if (valid) {
          await addEmployees(this.form);
          this.$emit('refresh');
        }
      });
    }
  }
};
</script>
<style lang="less" scoped></style>
